package com.sdhs.listmanage.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sdhs.common.annotation.Log;
import com.sdhs.common.core.controller.BaseController;
import com.sdhs.common.core.domain.AjaxResult;
import com.sdhs.common.enums.BusinessType;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.common.utils.poi.ExcelUtil;
import com.sdhs.listmanage.domain.entity.GcManageList;
import com.sdhs.listmanage.service.IGcManageListService;
import com.sdhs.listmanage.service.IGcManageListTemporaryService;

/**
 * 清单管理Controller
 *
 * @author ww
 */
@RestController
@RequestMapping("/manageList")
public class GcManageListController extends BaseController {
    @Autowired
    private IGcManageListService gcManageListService;

    @Autowired
    private IGcManageListTemporaryService gcManageListTemporaryService;

    /**
     * 查询清单管理树列表
     */
    @PreAuthorize("@ss.hasPermi('manageList:list')")
    @GetMapping("/treeList")
    public AjaxResult treeList(GcManageList gcManageList) {
        AjaxResult ajax = AjaxResult.success();
        /*gcManageList.setProcurementType("0");*/
        ajax.put("manageTreeList", gcManageListService.selectManageListTreeList(gcManageList));
        return ajax;
    }


    @PreAuthorize("@ss.hasPermi('manageList:import')")
    @Log(title = "导入清单管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, Long projectId) throws Exception {
        ExcelUtil<GcManageList> util = new ExcelUtil<>(GcManageList.class);
        List<GcManageList> manageList = util.importExcel(file.getInputStream(),1);
        String message = gcManageListService.importGcManageList(manageList, projectId, SecurityUtils.getUsername());
        return success(message);
    }

    /**
     * 清单管理暂存
     */
    @PreAuthorize("@ss.hasPermi('manageList:add')")
    @Log(title = "清单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/handleTransient")
    public AjaxResult handleTransient(@RequestBody List<GcManageList> gcManageList) {
        for (GcManageList gcManage : gcManageList) {
            gcManage.setIsDelete("0");
            gcManageListService.updateGcManageList(gcManage);
        }
        return toAjax(1);
    }
    /**
     * 新增清单管理
     */
    @PreAuthorize("@ss.hasPermi('manageList:add')")
    @Log(title = "清单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GcManageList gcManageList) {
        return toAjax(gcManageListService.insertGcManageList(gcManageList));
    }

    /**
     * 修改清单管理
     */
    @PreAuthorize("@ss.hasPermi('manageList:edit')")
    @Log(title = "清单管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult edit(@RequestBody GcManageList gcManageList) {
        return toAjax(gcManageListService.updateGcManageList(gcManageList));
    }

    /**
     * 锁定清单管理
     */
    @PreAuthorize("@ss.hasPermi('manageList:edit')")
    @Log(title = "锁定清单管理", businessType = BusinessType.UPDATE)
    @PutMapping("/lock")
    public AjaxResult update(GcManageList gcManageList) {
         int i = gcManageListService.update(gcManageList);
         /*清单锁定的时候将清单数据同步到清单备份表中*/
        /*获取清单数据*/
/*         List<GcManageList> gcManageLists = gcManageListService.selectGcManageListByProject(gcManageList.getProjectId());
         for (int j = 0; j < gcManageLists.size(); j++) {
             GcManageList gcManageList1 = gcManageLists.get(j);
             GcManageListTemporary manageList = BeanUtil.toBean(gcManageList1, GcManageListTemporary.class);
             gcManageListTemporaryService.insertGcManageList(manageList);
        }*/
        return toAjax(i);
    }
    /**
     * 根据id删除清单管理
     */
    @PreAuthorize("@ss.hasPermi('manageList:remove')")
    @Log(title = "清单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(gcManageListService.deleteGcManageListById(id));
    }

}
